package com.heima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.dto.InsertUserDto;
import com.heima.dto.UserResetDto;
import com.heima.dto.UserStatusDto;
import com.heima.dto.*;
import com.heima.dto.pagedto.UserPageDto;
import com.heima.entity.Dept;
import com.heima.entity.UserInfo;
import com.heima.mapper.DeptMapper;
import com.heima.mapper.UserInfoMapper;
import com.heima.mapper.UserMapper;
import com.heima.result.ListRoleResult;
import com.heima.service.UserService;
import com.heima.vo.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;
    private final DeptMapper deptmapper;
    private final UserInfoMapper userInfoMapper;

    /**
     * 用户分页查询
     * @param dto
     * @return
     */
    @Override
    public PageResult queryUser(UserPageDto dto) {
        log.info("用户分页查询条件：{}",dto);
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        Page<UserVo> list = userMapper.queryUser(dto);
        //获取用户的部门id列表
        Set<Integer> deptIds = list.getResult().stream()
                .map(UserVo::getDeptId)
                .collect(Collectors.toSet());
        List<Integer> integerList = new ArrayList<>(deptIds);
        // 批量查询部门信息
        List<DeptVo> departments = deptmapper.findByIds(integerList);

        // 将部门信息映射到UserVo对象
        Map<Integer, DeptVo> departmentMap = departments.stream()
                .filter(deptVo -> deptVo.getDeptId() != null)
                .collect(Collectors.toMap(DeptVo::getDeptId, Function.identity()));

        list.getResult().forEach(l -> {
            DeptVo dept = departmentMap.get(l.getDeptId());
            l.setDept(dept); // 设置UserVo的部门信息
        });


        PageResult pageResult = new PageResult();
        pageResult.setTotal(list.getTotal());
        pageResult.setRows(list.getResult());
        return pageResult;
    }

    /**
     * 用户修改状态
     * @param dto
     */
    @Override
    public void updateStatus(UserStatusDto dto) {
        userMapper.updateStatus(dto);
    }

    /**
     * 根据id查询用户信息
     * @param id
     * @return
     */
    @Override
    public UserVo findUserById(Integer id) {
        UserVo vo = userMapper.findUserById(id);
        return vo;
    }

    /**
     * 新增用户
     * @param dto
     */
    @Override
    public void insertUser(InsertUserDto dto) {
        userMapper.insertUser(dto);
    }

    /**
     * 重置用户密码
     * @param dto
     */
    @Override
    public void resetPassword(UserResetDto dto) {
        userMapper.resetPassword(dto);
    }

    /**
     * 通过部门id查询用户
     * @param deptId
     * @return
     */
//    @Override
//    public List<UserDto> queryUserByDeptId(Integer deptId) {
//        return userMapper.queryUserByDeptId(deptId);
//    }

    /**
     * 获取角色列表
     */
    @Override
    public ListRoleResult listselect() {
        List<RoleVo> list =userMapper.listselect();
        ListRoleResult listRole = new ListRoleResult();
        listRole.setRoles(list);
        return listRole;
    }

    @Override
    public List<UserInfoDeptVo> queryDeptUser(Integer deptId) {
        List<UserInfo> userInfoList =  userInfoMapper.queryUserInfoByDeptId(deptId);
        Dept dept = deptmapper.queryById(Long.valueOf(deptId));
        DeptVo deptVo = new DeptVo();
        BeanUtils.copyProperties(dept, deptVo);

        List<UserInfoDeptVo> userInfoDeptVoList = new ArrayList<>();
        for (UserInfo userInfo : userInfoList) {
            UserInfoDeptVo userInfoDeptVo = new UserInfoDeptVo();
            BeanUtils.copyProperties(userInfo, userInfoDeptVo);
            userInfoDeptVo.setDeptVo(deptVo);
            userInfoDeptVoList.add(userInfoDeptVo);
        }

        return userInfoDeptVoList;
    }

    /**
     * 根据id删除用户
     * @param id
     */
    @Override
    public void deleteUser(Integer id) {
        userMapper.deleteUser(id);
    }

    /**
     * 修改用户
     * @param dto
     */
    @Override
    public void updateUser(InsertUserDto dto) {
        userMapper.updateUser(dto);
    }


//    /**
//     * 用户登录
//     * @param loginDto
//     * @return
//     */
//    @Override
//    public User login(UserLoginDto loginDto) {
//        return null;
//    }
}
